<template>
  <div class="app-container">
    <el-form :model="queryForm" ref="queryFormRef" :inline="true" v-show="showSearch" label-width="68px">
<#list queryList as item>
			<el-form-item label="${item.attrLabel!}" prop="${item.attrName}">
			  <#if item.htmlType == 'input' || item.htmlType == 'textarea' || item.htmlType == 'summernote' >
			  <el-input
			    v-model="queryForm.${item.attrName}"
			    placeholder="${item.attrLabel!}"
			    clearable
			    @keyup.enter.native="queryHandle"
			    >
			  </el-input>
			  <#elseif item.htmlType == 'select'>
			    <#if item.dictType?? && item.dictType != "">
			  <fast-select
			    v-model="queryForm.${item.attrName}"
			    dict-type="${item.dictType}"
			    placeholder="${item.attrLabel!}"
			    clearable
			    @keyup.enter.native="queryHandle"
			    >
			  </fast-select>
			    <#else>
			  <el-select
			    v-model="queryForm.${item.attrName}"
			    placeholder="${item.attrLabel!}"
			    clearable
			    @keyup.enter.native="queryHandle"
			    >
				<el-option label="选择" value="0"></el-option>
			  </el-select>
			    </#if>
			  <#elseif item.htmlType == 'radio'>
			    <#if item.dictType?? && item.dictType != "">
			  <fast-radio-group
			    v-model="queryForm.${item.attrName}"
			    dict-type="${item.dictType}"
          @keyup.enter.native="queryHandle"
			    >
			  </fast-radio-group>
			    <#else>
			  <el-radio-group
			    v-model="queryForm.${item.attrName}"
			    @keyup.enter.native="queryHandle"
			    >
				<el-radio :label="0">单选</el-radio>
			  </el-radio-group>
			    </#if>
			  <#elseif item.htmlType == 'date'>
			  <el-date-picker
          v-model="daterange"
          size="small"
          type="daterange"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          @keyup.enter.native="queryHandle"
          >
			  </el-date-picker>
			  <#elseif item.htmlType == 'datetime'>
			  <el-date-picker
          v-model="datetimerange"
          size="small"
          type="datetimerange"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          @keyup.enter.native="queryHandle"
          >
			  </el-date-picker>
			  <#else>
			  <el-input
			    v-model="queryForm.${item.attrName}"
			    placeholder="${item.attrLabel!}"
			    clearable
			    @keyup.enter.native="queryHandle"
			    >
			  </el-input>
			  </#if>
			</el-form-item>
      </#list>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="addOrUpdateHandle()"
          v-hasPermi="['${moduleName}:${beanName}:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="deleteHandle"
          v-hasPermi="['${moduleName}:${beanName}:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
          v-hasPermi="['${moduleName}:${beanName}:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getPage"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pageVO.rows" @selection-change="selectionChangeHandle">
      <el-table-column type="selection" width="55" align="center" />
    <#list gridList as item>
      <#if item.dictType?? && item.dictType != "">
      <fast-table-column prop="${item.attrName}" label="${item.attrLabel!}" dict-type="${item.dictType}" />
      <#else>
      <el-table-column prop="${item.attrName}" label="${item.attrLabel!}" header-align="center" align="center" />
      </#if>
    </#list>
      <el-table-column label="操作" header-align="center" align="center" fixed="right" width="200" >
        <template slot-scope="{row}">
          <el-button
            type="text"
            icon="el-icon-edit"
            @click="addOrUpdateHandle(row.id)"
            v-hasPermi="['${moduleName}:${beanName}:edit']"
          >修改</el-button>
          <el-button
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(row)"
            v-hasPermi="['${moduleName}:${beanName}:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="pageVO.total>0"
      :total="pageVO.total"
      :page.sync="queryForm.page"
      :limit.sync="queryForm.limit"
      @pagination="getPage"
    />

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getPage" />
  </div>
</template>

<script>
import * as ${beanName}Api from "@/api/${moduleName}/${beanName}";
import AddOrUpdate from './add-or-update.vue'

export default {
  name: "${beanName}",
  dicts: ['health_state'],
  components: { AddOrUpdate },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 返回参数
      pageVO: {
        rows: [], // 返回的列表
        total: 0, // 一共多少条数据
      },

      // 日期范围
      dateRange: [],
      // 查询参数
      queryForm: {
        page: 1,
        limit: 10,
        <#list queryList as item>
        ${item.attrName}: undefined<#sep>,
        </#list>
      },
      addOrUpdateVisible: false,
    };
  },
  created() {
    this.getPage();
  },
  methods: {
    /** 查询参数列表 */
    getPage() {
      this.addOrUpdateVisible = false;
      this.loading = true;
      ${beanName}Api.page(this.addDateRange(this.queryForm, this.dateRange)).then(res => {
          this.pageVO = res.data;
          this.loading = false;
        }
      );
    },

    /** 搜索按钮操作 */
    queryHandle() {
      this.queryForm.page = 1;
      this.getPage();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.$refs.queryFormRef.resetFields()
      this.queryHandle();
    },

    /** 新增按钮操作 */
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },

    // 多选框选中数据
    selectionChangeHandle(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    /** 删除按钮操作 */
    deleteHandle(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除参数编号为"' + ids + '"的数据项？').then(function() {
          return ${beanName}Api.remove(ids);
        }).then(() => {
          this.getPage();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },

    /** 导出按钮操作 */
    exportHandle() {
      this.download('${moduleName}/${beanName}/export', {
        ...this.queryForm
      <#noparse>
      }, `doc_${new Date().getTime()}.xlsx`)
      </#noparse>
    },
  }
};
</script>